﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Library.Helper
{
    public abstract partial class CmdHelper : EncryptHelper
    {
        public static Action<string> LogAction = msg => { Console.WriteLine(msg); };

        /// <summary>
        /// 启动cmd
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public virtual string[] RunCmd(string input)
        {
            var process = new Process();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
            process.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
            process.StartInfo.RedirectStandardError = true; //重定向标准错误输出
            process.StartInfo.CreateNoWindow = true; //不显示程序窗口
            try
            {
                process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                process.StartInfo.FileName = "cmd";
                process.Start(); //启动程序

                process.StandardInput.WriteLine(input + "&exit");
                process.StandardInput.AutoFlush = true;

                var res = new List<string>();
                process.BeginOutputReadLine();
                process.OutputDataReceived += (a, b) => { if (string.IsNullOrEmpty(b.Data)) return; res.Add(b.Data); };
                process.WaitForExit();
                LogAction(res[2].Replace("&exit", ""));
                return res.Skip(3).ToArray();
            }
            finally
            {
                process.Close();
            }
        }
    }

    public partial class CmdHelper
    {
        /// <summary>
        /// 直接启动对应程序
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="input"></param>
        public virtual void RunFile(string fileName = null, string arguments = "")
        {
            var process = new Process();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
            process.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
            process.StartInfo.RedirectStandardError = true; //重定向标准错误输出
            process.StartInfo.CreateNoWindow = true; //不显示程序窗口

            process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
            process.StartInfo.FileName = fileName;
            process.StartInfo.Arguments = arguments;

            LogAction(string.Format("{0} {1} {2}", "", process.StartInfo.FileName, process.StartInfo.Arguments));

            process.Start();

            try
            {
                process.BeginErrorReadLine();
                process.ErrorDataReceived += (a, b) => { if (string.IsNullOrEmpty(b.Data)) return; LogAction(b.Data); };
                process.BeginOutputReadLine();
                process.OutputDataReceived += (a, b) => { if (string.IsNullOrEmpty(b.Data)) return; LogAction(b.Data); };

                process.WaitForExit();
                if (process.ExitCode < 0)
                    LogAction("error");
            }
            finally
            {
                process.Close();
            }
        }
    }
}